/*
 * Copyright (c) 2017. 上海业晋信息技术有限公司 www.yejinsoft.com
 */

//config的设置是全局的
layui.config({
    base: '/js/plugins/' // 自定义扩展插件所在的目录
}).extend({ //设定模块别名
    yjsloading: 'yjsloading',
});

layui.use(['form', 'yjsloading'], function () {
    var $ = layui.jquery
        , form = layui.form
        , yjsloading = layui.yjsloading

    // 自定义表单验证规则
    form.verify({
    });

    // 重新获取验证码
    $('.getLVcode').click(function () {
        var url = '/vcode/path/form';
        yjsloading.loadingCaptcha(url);
    });

    // 监听迎新信息表单确定按钮事件
    form.on('submit(toolbarEvent)', function (data) {
        var eventType = data.field.viewType;
        var url, msg = '';

        switch (eventType) {
            case "addData":
                url = '/newarrival/add';
                msg = '您确定执行本次数据新增操作吗？';
                break;
        }

        $title = '迎新换届维护';
        $url = url;
        $msg = msg;
        yjsloading.ajaxConfirm($title, $msg, $url, data.field, function (result) {
            $('#rspStatus').val(result['rspStatus']);
            $('#rspMsg').val(result['rspMsg']);
        });
    });




    // 事件处理
    var eventHandle = {
        // 生成迎新代码
        createNewArrivalCode: function (obj) {
            var enrollment_year = $('#enrollment_year').val();
            var enrollment_term = $('#enrollment_term').val();
            var enrollment_serial = $('#enrollment_serial').val();
            var years = new Array(); //定义一数组
            years = enrollment_term.split('-');
            var newArrivalCode = '';
            var lastStr = '00';
            var serial = '00';
            if (years.length>2){
                lastStr = years[2];
                if (lastStr.length==1){
                    lastStr = "0" + lastStr;
                }
                if (enrollment_serial.length==1){
                    serial = "0" + enrollment_serial;
                }else  {
                    serial = enrollment_serial;
                }
                newArrivalCode = years[0] + years[1] + lastStr + serial;
            }else  {
                newArrivalCode = enrollment_year+enrollment_term+enrollment_serial;
            }
            $("#newArrivalCode").val(newArrivalCode);
        }
    };

    // 监听个人信息表单取消按钮事件
    $('.cancelEvent').click(function () {
        $title = '迎新换届维护';
        $msg = '您确定取消本次数据更新操作吗？';
        yjsloading.cancelConfirm($title, $msg);
    });

    // // 监听按钮点击事件 (非工具条)
    $('.layui-btn').on('click', function () {
        var type = $(this).data('type');
        eventHandle[type] ? eventHandle[type].call(this, type) : '';
    });
});